<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>解构赋值(destructuring assignment)</title>
</head>
<body>

<p>解构赋值是对赋值运算符的扩展。</p>

<p>  是一种针对数组或者对象进行模式匹配，然后对其中的变量进行赋值。</p>

<p>省去json解析的步骤</p>
<P>参考文档：  http://es6.ruanyifeng.com/#docs/destructuring</P>

<script>
    //数组赋值
    let [a, b, c] = [10, 20, 30];
    console.log(a, b, c); //10 20 30

    let [x, y, ...other] = [1,2,3,4,5];
    console.log(x, y, other); //1 2 [ 3, 4, 5 ]

    //字符串等
    let [a1, b1, c1, d1, e1] = 'hello';
    // a1 = 'h'
    // b1 = 'e'
    // c1 = 'l'
    // d1 = 'l'
    // e1 = 'o'
    console.log("a1:"+a1+" b1:"+b1+" c1:"+c1+" d1:"+d1+" e1:"+e1);//a1:h b1:e c1:l d1:l e1:o


    //对象赋值
    let {name, age} = { name: 'Koma', age: 20 };
    console.log(name, age);//Koma 20

    //函数赋值
    function func1() {
        return [10, 20];
    }
    let [num1, num2] = func1();
    console.log(num1, num2);//10 20

    //函数参数名指定
    function func2({x=1, y=2}){
        return x+y;
    }
    console.log(func2({}));           //3
    console.log(func2({x:10}));       //12
    console.log(func2({y:10}));       //11
    console.log(func2({x:10, y:20})); //30
</script>
</body>
</html>
